package com.qingluan.DataFormat;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qingluan.Database.Personal_Info_database;
import com.qingluan.arguments.ARGUMENTS;
import com.qingluan.dd.InetTool.BitmapDownloader;
import com.qingluan.dd.InetTool.BitmapDownloader.CallBack;
import com.qingluan.dd.InetTool.HttpPost;
import com.qingluan.dd.InetTool.HttpPost.POSTListener;
import com.qingluan.widget.BitmapHelper;

public class PersonTag {
	//person change argus area
	public static final String CHANGE_NAME = "name";
	public static final String CHANGE_PASS = "password";
	public static final String CHANGE_DESCRIBE = "describe";
	public static final String CHANGE_PICTURE = "picture";
	
	private static String tag = PersonTag.class.getName();
	
	//context argu area
	private Context context_from_activity;
	private String this_email;
	
	public PersonTag(Context context,String uid)  {
		// TODO Auto-generated constructor stub
		context_from_activity = context;
		this_email = new Personal_Info_database(context_from_activity).searchEmail(uid);
	}
	
	public  void LoadOwnInfo(String token_id,String token_key,final TextView nameView,final ImageView my_picture) {
		JSONObject jsonObject = new JSONObject();
		
		try {
			jsonObject.put("tag", "email");
			jsonObject.put("info", this_email);
			jsonObject.put("user_id", token_id);
			jsonObject.put("user_key", token_key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d(tag,jsonObject.toString());
		Log.d(tag, "stat ... down..");
		HttpPost connection = new HttpPost(ARGUMENTS.GET_OWN_PERSON_INFO_IP);
		connection.setPOSTTemplateListener(new POSTListener() {
			
			@Override
			public void successful(String info) {
				// TODO Auto-generated method stub
				JSONObject person_info = null;
				try {
					person_info = new JSONObject(info);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				String response =null;
				try {
					response = person_info.getString("response");
					if (response.equals("ok")){
						String name = person_info.getString("name");
						String describe = person_info.getString("describe");
						final String my_phone = person_info.getString("phone");
						final String picture_path = person_info.getString("picture_path");
						
						ContentValues valuesOnDB = new ContentValues();
						
						//DB saving...
						valuesOnDB.put(Personal_Info_database.PERSON_NAME, name);
						valuesOnDB.put(Personal_Info_database.PERSON_DESCRIBE, describe);
						valuesOnDB.put(Personal_Info_database.PERSON_UID_PHONE, my_phone);
						
						new Personal_Info_database(context_from_activity).update(valuesOnDB, my_phone);
						
						//view setting ...
						nameView.setText(name);
//						describeView.setText(describe);
						
						
						if (!picture_path.equals("null")){
							BitmapDownloader.Download(context_from_activity, picture_path, new CallBack() {
								
								@Override
								public void onDataGot(byte[] data) {
									// TODO Auto-generated method stub
									String picLocalPath = BitmapHelper.SaveImage(picture_path, data);
									Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
									
									ContentValues valuesOnDB = new ContentValues();
									valuesOnDB.put(Personal_Info_database.PERSON_PICTURE, picLocalPath);
									new Personal_Info_database(context_from_activity).update(valuesOnDB, my_phone);
									my_picture.setImageBitmap(picture);
								}
							});
						}else{
							
							Log.d(tag,"you have not update your picture!!");
						}
						
					}else if(response.equals("id_key_expired")){
						Toast.makeText(context_from_activity, "token  expired !!", Toast.LENGTH_SHORT).show();
						
					}else{
						
						Log.d(tag, "fail to get..");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
			}
			
			@Override
			public void error(String error) {
				// TODO Auto-generated method stub
				Toast.makeText(context_from_activity, error, Toast.LENGTH_SHORT).show();
			}
		});
		
		
		connection.JSONPOSTTemplate(ARGUMENTS.GET_OWN_PERSON_INFO, jsonObject);
	}

	public void ChangeOwnInfo(String chaneg_type,String change_info,String token_id,String token_key,final ChangeListener changeListener) throws JSONException{
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		
		jsonArray.put(chaneg_type);

//		jsonObject.put("tag", "email");
		jsonObject.put("email", this_email);
		jsonObject.put("user_id",token_id);
		jsonObject.put("user_key", token_key);
		jsonObject.put("changes", jsonArray);
		jsonObject.put("change_"+chaneg_type, change_info);
		
		Log.d(tag,"email :"+this_email);
		
		HttpPost connection = new HttpPost(ARGUMENTS.CHANGE_PROFILE_IP);
		connection.setPOSTTemplateListener(new POSTListener() {
			
			@Override
			public void successful(String info) {
				// TODO Auto-generated method stub
				try {
					JSONObject jsonInfo = new JSONObject(info);
					String result = jsonInfo.getString("response");
					changeListener.result(result);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			@Override
			public void error(String error) {
				// TODO Auto-generated method stub
				
			}
		});
		
		connection.JSONPOSTTemplate(ARGUMENTS.POST_CHANGE_PROFILE, jsonObject);
	}
	
	
	public interface ChangeListener{
		public void result(String info);
	}

}
